package org.inspiration.dudu.web.service.user.impl;

import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.common.constant.Constant;
import org.inspiration.dudu.common.exception.ServiceException;
import org.inspiration.dudu.content.model.vo.AnswerVO;
import org.inspiration.dudu.content.model.vo.ArticleVO;
import org.inspiration.dudu.content.model.vo.QuestionVO;
import org.inspiration.dudu.content.service.AnswerService;
import org.inspiration.dudu.content.service.ArticleService;
import org.inspiration.dudu.content.service.QuestionService;
import org.inspiration.dudu.user.model.dto.UserFavoriteDTO;
import org.inspiration.dudu.user.model.entity.UserFavoriteEntity;
import org.inspiration.dudu.user.service.FavoriteFolderService;
import org.inspiration.dudu.user.service.UserFavoriteService;
import org.inspiration.dudu.web.service.user.UserFavoriteFacadeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class UserFavoriteFacadeServiceImpl implements UserFavoriteFacadeService {

    private final UserFavoriteService userFavoriteService;
    private final ArticleService articleService;
    private final QuestionService questionService;
    private final AnswerService answerService;
    private final FavoriteFolderService favoriteFolderService;

    /**
     * 创建用户和内容的收藏关系
     * 
     * @param userFavoriteDTO 用户收藏数据传输对象
     */
    @Override
    @Transactional
    public void createUserFavorite(UserFavoriteDTO userFavoriteDTO) {
        userFavoriteService.createUserFavorite(userFavoriteDTO);
        String contentId = userFavoriteDTO.getContentId();
        // 增加对应内容的收藏量
        switch (userFavoriteDTO.getContentType()) {
            case "article" -> {
                articleService.increaseFavoriteCount(contentId);
            }
            case "question" -> {
                questionService.increaseFavoriteCount(contentId);
            }
            case "answer" -> {
                answerService.increaseFavoriteCount(contentId);
            }
            default -> {
                throw new ServiceException(Constant.CONTENT_TYPE_ERROR);
            }
        }
        // 增加相应收藏夹的内容数量
        favoriteFolderService.increaseContentCount(userFavoriteDTO.getFolderId());
    }

    /**
     * 删除用户和内容的收藏关系
     * 
     * @param userFavoriteDTO 用户收藏数据传输对象
     */
    @Override
    @Transactional
    public void deleteUserFavorite(UserFavoriteDTO userFavoriteDTO) {
        userFavoriteService.deleteUserFavorite(userFavoriteDTO);
        // 减少对应内容的收藏量
        String contentId = userFavoriteDTO.getContentId();
        switch (userFavoriteDTO.getContentType()) {
            case "article" -> {
                articleService.decreaseFavoriteCount(contentId);
            }
            case "question" -> {
                questionService.decreaseFavoriteCount(contentId);
            }
            case "answer" -> {
                answerService.decreaseFavoriteCount(contentId);
            }
            default -> {
                throw new ServiceException(Constant.CONTENT_TYPE_ERROR);
            }
        }
        // 减少相应收藏夹的内容数量
        favoriteFolderService.decreaseContentCount(userFavoriteDTO.getFolderId());
    }

    /**
     * 检查用户是否收藏了指定内容
     * 
     * @param userId 用户ID
     * @param contentId 内容ID
     * @return 是否收藏
     */
    @Override
    public Boolean isFavorite(Long userId, String contentId) {
        return userFavoriteService.isFavorite(userId, contentId);
    }

    /**
     * 获取用户收藏的内容
     * 
     * @param userId 用户ID
     * @param favoriteFolderId 收藏夹ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 收藏内容列表
     */
    @Override
    public List<Map<String, Object>> getFavoriteContents(
            Long userId, Long favoriteFolderId,
            Integer pageNum, Integer pageSize) {
        List<Map<String, Object>> list = new ArrayList<>();
        // 先获取关系列表
        List<UserFavoriteEntity> userFavoriteEntities = userFavoriteService.getUserFavoriteByUserId(userId, favoriteFolderId, pageNum, pageSize);
        // 遍历关系列表查询具体内容
        for(UserFavoriteEntity userFavoriteEntity: userFavoriteEntities) {
            String contentType = userFavoriteEntity.getContentType();
            String contentId = userFavoriteEntity.getContentId();
            LocalDateTime favoritedAt = userFavoriteEntity.getFavoritedAt();
            Map<String, Object> map = new HashMap<>();
            map.put("contentType", contentType);
            map.put("favoritedAt", favoritedAt);
            switch (contentType) {
                case Constant.ARTICLE_TYPE -> {
                    ArticleVO articleVO = articleService.getArticleById(contentId);
                    map.put("content", articleService.transToSimpleVO(articleVO));
                }
                case Constant.QUESTION_TYPE -> {
                    QuestionVO questionVO = questionService.getQuestionById(contentId);
                    map.put("content", questionVO);
                }
                case Constant.ANSWER_TYPE -> {
                    AnswerVO answerVO = answerService.getAnswerByContentId(contentId);
                    QuestionVO questionVO = questionService.getQuestionById(answerVO.getParentId());
                    map.put("content", answerVO);
                    map.put("question", questionVO);
                }
                default -> {
                    throw new ServiceException(Constant.CONTENT_TYPE_ERROR);
                }
            }
            list.add(map);
        }
        return list;
    }

    /**
     * 检查用户是否在指定收藏夹中收藏了内容
     * 
     * @param userId 用户ID
     * @param contentId 内容ID
     * @param folderId 收藏夹ID
     * @return 是否收藏
     */
    @Override
    public Boolean isFolderFavorite(Long userId, String contentId, Long folderId) {
        return userFavoriteService.isFolderFavorite(userId, contentId, folderId);
    }
}
